import {Component, EventEmitter, Input, OnInit, Output} from "@angular/core";
import {ViewCell} from "ng2-smart-table";
import {NbDialogRef, NbDialogService} from "@nebular/theme";

@Component({
  template: `
    <a href="javascript:void(0)" (click)="openDialog()"><i class="nb-edit"></i>{{renderValue}}</a>
  `,
})
export class UURBindDialogRender implements ViewCell, OnInit {
  renderValue: string;

  @Input() value: string | number;
  @Input() rowData: any;
  @Output() save: EventEmitter<any> = new EventEmitter();
  constructor(private dialog:NbDialogService){

  }

  openDialog(){
    this.dialog.open(UURBindDialog,{
      context:{
        unitId: this.rowData['unitId'],
        uurId: this.rowData['uurId'],
        userId:this.rowData['userId'],
      }
    }).onClose.subscribe(result=>{
      this.save.emit(result)
    })
  }

  ngOnInit() {
    this.renderValue = this.value.toString()
  }

}

@Component({
  template: `
    <nb-card style="min-width:800px">
      <nb-card-header>用户单元绑定</nb-card-header>
      <nb-card-body>
        <div class="form-group">
          <label>用户ID</label>
          <input nbInput fullWidth [(ngModel)]="userId" disabled>
          <label>单元ID</label>
          <input nbInput fullWidth [(ngModel)]="unitId">
          <label>额外信息</label>
          <input nbInput fullWidth [(ngModel)]="extInfo">
        </div>
      </nb-card-body>
      <nb-card-footer>
        <button nbButton status="danger" (click)="cancel()">取消</button>
        <button nbButton status="success"
                (click)="submit()">
          确定
        </button>
      </nb-card-footer>
    </nb-card>
  `,
  styles:[
    `
    button {
      margin:5px;
    }
    `
  ]
})
export class UURBindDialog implements OnInit{
  @Input('userId') userId:number;
  @Input('uurId') uurId:number;
  @Input("unitId") unitId: number;
  extInfo:string='';
  oldUnitId:number;
  constructor(protected ref: NbDialogRef<UURBindDialog>) {
  }

  ngOnInit(): void {
    this.oldUnitId = this.unitId;
  }

  cancel() {
    this.ref.close();
  }

  submit() {
    this.ref.close({uurId:this.uurId,userId:this.userId,unitId:this.unitId,oldUnitId:this.oldUnitId,extInfo:this.extInfo});
  }
}
